package com.fyy.scheduled.controller;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.time.LocalDateTime;

/**
 * @Author fyy
 * @Description
 * @Date 2020/12/18 9:37
 */
@Controller
@RequestMapping(value = "/async/task/")
public class AsyncScheduledController {

    @RequestMapping(value = "test")
    public void test(){
        System.out.println("异步定时任务测试");
    }

    /**
     * @Author fyy
     * @Description 异步任务测试,
     *    没开启异步时：所有的任务都会在SpringBoot默认的大小为1的线程池中执行
     *        任务是串行执行的，执行周期会受方法内的时间影响
     *    开启异步后：任务全部并行执行，不会受方法内的时间影响
     * @Date 2020/12/18 9:27
     */
    @Async
    @Scheduled(fixedDelay = 2000)
    public void fixedDelayTask() throws InterruptedException {
        Thread.sleep(20000);
        System.out.println("Fixed Delay Task，Current Thread : " + Thread.currentThread().getName() + ",执行时间：" + LocalDateTime.now());
    }

}
